/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.StockCode;
import com.openbrew.openbrewrf.Wearhouse;
import com.openbrew.openbrewrf.Yeast;
import com.openbrew.openbrewrf.views.YeastView;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import service.enumerations.WarehouseIngredient;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.yeast")
public class YeastFacadeREST extends AbstractFacade<Yeast> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public YeastFacadeREST() {
        super(Yeast.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public Yeast create(@WebParam(name = "yeast") Yeast entity) {
        if(entity.getStockCode() != null){
            StockCode sc = entity.getStockCode();
            if(sc.getWhid()==null){
                sc.setWhid(getYeastWarehouse(em));
            }
            getEntityManager().persist(sc);
            getEntityManager().flush();
            getEntityManager().refresh(sc);
            entity.setStockCode(sc);
            entity = super.create(entity);
            sc.setTableID(entity.getYid());
            getEntityManager().merge(sc);
        } else {
            StockCode sc = new StockCode();
            sc.setStockAltVendID(null);
            sc.setStockVendID(null);
            sc.setTableID(0);
            if(sc.getWhid()==null){
                sc.setWhid(getYeastWarehouse(em));
            }
            getEntityManager().persist(sc);
            getEntityManager().flush();
            getEntityManager().refresh(sc);
            entity.setStockCode(sc);
            entity = super.create(entity);
            sc.setTableID(entity.getYid());
            getEntityManager().merge(sc);
        }
        getEntityManager().getEntityManagerFactory().getCache().evict(YeastView.class);
        return entity;
    }
    
    private Wearhouse getYeastWarehouse(final EntityManager en){
        TypedQuery<Wearhouse> query = en.createQuery(
                "SELECT w FROM Wearhouse w WHERE w.wearhouseID = ?1", Wearhouse.class);
        query.setParameter(1, WarehouseIngredient.YEAST.getDbId());
        Wearhouse warehouse = query.getSingleResult();
        return warehouse;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Yeast entity) {
        super.edit(entity);
        em.merge(entity.getStockCode());
        getEntityManager().getEntityManagerFactory().getCache().evict(YeastView.class);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
        getEntityManager().getEntityManagerFactory().getCache().evict(YeastView.class);
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Yeast find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Yeast> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Yeast> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
